package com.baizhi.service.impl;

import com.baizhi.dto.response.StudentScoreDTO;
import com.baizhi.entity.Course;
import com.baizhi.entity.Student;
import com.baizhi.entity.StudentCourse;
import com.baizhi.entity.User;
import com.baizhi.exception.CodeEnum;
import com.baizhi.mapper.CourseMapper;
import com.baizhi.mapper.StudentMapper;
import com.baizhi.mapper.UserMapper;
import com.baizhi.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
* @author 16357
* @description 针对表【t_course】的数据库操作Service实现
* @createDate 2024-08-14 10:13:27
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Optional<Course> findById(Integer pkCid) {
        Course course = courseMapper.selectById(pkCid);
        /**
         * Optional 中有一个方法 isParent ,返回一个boolean值，用于判断这个对象是否为空
         */
        Optional<Course> optionalCourse = Optional.ofNullable(course);
        return optionalCourse;
    }

    @Override
    public Integer update(Course course) {
        //判断课程是否存在
        Optional<Course> o = findById(course.getPkCid());
        /*
        isPresent() 如果Optional中包含值则返回true，如果不包含则返回false
         */
        if(!o.isPresent()){
            //要修改的课程不存在
            CodeEnum.course_is_not_exit.throwException();
        }
        //可成存在，修改内容
        Course result = o.get();
       result.setCourseName(course.getCourseName());
       result.setStartTime(course.getStartTime());
       result.setCategory(course.getCategory());

       result.setGmtModified(new Date());

        int row = courseMapper.updateById(result);

        if(row == 0){
            //修改失败
            CodeEnum.update_score_fail.throwException();
        }
        return row;
    }

    @Override
    public List<StudentScoreDTO> queryScoreByUserIdAndCourseId(Integer userId) {
        //获取学生ID
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).select("pk_sid");
        Student student = studentMapper.selectOne(wrapper);

        List<StudentScoreDTO> studentScoreDTOS = courseMapper.queryScoreByStudentIdAndCourseId(student.getPkSid());
        if(studentScoreDTOS ==null || studentScoreDTOS.size() ==0 ){
            CodeEnum.select_course_score.throwException(); //查询成绩的课程不存在
        }
        return studentScoreDTOS;

    }

    @Override
    public Integer deleteMyCourseByUserIdAndCourseId(Integer userId, Integer courseId) {
        //把用户ID转换成学生ID
        //获取学生ID
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).select("pk_sid");
        Student student = studentMapper.selectOne(wrapper);

        Integer row = courseMapper.deleteMyCourseByStuIdAndCourseId(student.getPkSid(), courseId);
        if(row == 0){
            CodeEnum.cancel_a_course_fail.throwException(); //取消选课失败
        }
        return row;
    }

    @Override
    public List<Course> getCoursesByUserId(Integer userId) {
        // 判断用户是否存在
        User user = userMapper.selectById(userId);
        if(user == null){
            CodeEnum.user_is_no_regist.throwException(); //用户未注册
        }
        //获取学生ID
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).select("pk_sid");
        Student student = studentMapper.selectOne(wrapper);

        List<Course> courses = courseMapper.queryMyCourseByStudentId(student.getPkSid());
        if(courses == null){
            //该学生还没有选课
            CodeEnum.student_not_select_course.throwException();
        }

        return courses;
    }

    @Override
    public Integer addCourse(Integer userId, Integer courseId) {
        Course course = courseMapper.selectById(courseId);
        if(course == null){
            //所选课程不存在
            CodeEnum.course_is_not_exit.throwException();//所选课程不存在
        }
        User user = userMapper.selectById(userId);
        if(user == null){
            //用户未注册
            CodeEnum.user_is_no_regist.throwException();
        }

        //我们需要的不是用户ID，而是stuId，所以根据userId查询stuId
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).select("pk_sid");
        Student student = studentMapper.selectOne(wrapper);

        //判断课程是否重复
        StudentCourse sc = courseMapper.queryCourseByStudentIdAndCourseId(student.getPkSid(), courseId);
        if(sc!=null){
            //课程不能重复选择
            CodeEnum.course_not_repetition.throwException();
        }
        Integer row = courseMapper.addCourse(student.getPkSid(), courseId);
        if(row==0){
            CodeEnum.selectcourse_is_fail.throwException();
        }

        return row;
    }

}




